/**
 * 
 * @param {进程名称} name 
 * @param {到达时间} arriveTime 
 * @param {需要服务时间} needTime 
 * @param {优先级} prio 
 * @param {剩余执行时间} moreTime 
 * @param {开始时间} beginTime 
 * @param {完成时间} finishTime 
 * @param {进程状态} status  4种状态：执行--0、就绪--1、阻塞--2、完成--3
 */

//等待队列
var Que=[];

//当前运行进程
var  curProcess=[];

//当前运行时间
var timeCount=0;

function Process(name,arriveTime,needTime,moreTime,beginTime,finishTime,prio,status){
    this.name=name;
    this.arriveTime=arriveTime;
    this.needTime=needTime;
    this.moreTime=moreTime;
    this.beginTime=beginTime;
    this.finishTime=finishTime;
    this.prio=prio;
    this.status=status;
}


//入等待队列
function putQue(process){
    //Que[Que.length]=process;
    Que.push(process)
}

//调度算法(选出下一个需要运行的进程)  非抢占式
//1.FIFO(先进先出)=>选出到达最早且还需要服务的进程
function popFIFO(){
    console.log("先进先出算法运行")
    //遍历等待序列
    var firstProcess=Que[0];
    
    //如果当前运行进程还没有运行完，直接返回当前进程
    if(curProcess[0]!=null && curProcess[0].needTime!=0){
            return curProcess[0];
    }
   
    //如果当前进程运行完了，从等待队列中找出下一个到达时间最早的程序
    for(var i=0;i<Que.length;i++){
        if(Que[i].needTime!=0){
            if(Que[i].arriveTime<=firstProcess.arriveTime){
                firstProcess=Que[i];
                //在等待队列中删除该元素
                Que.splice(i,1);
            }
        }
    }
    curProcess[0]=firstProcess;
    return firstProcess;
}


//2.短作业优先
function popSJF(){
    console.log("短作业优先算法运行")

    //遍历等待序列
    var firstProcess=Que[0];

    //如果当前运行进程还没有运行完，直接返回当前进程
    if(curProcess[0]!=null && curProcess[0].needTime!=0){
        return curProcess[0];
    }

    //如果当前进程运行完了，从等待队列中找出下一个所需服务时间最少的程序
    for(var i=0;i<Que.length;i++){
        if(Que[i].needTime!=0){
            if(Que[i].needTime<=firstProcess.needTime){
                firstProcess=Que[i];
                //在等待队列中删除该元素
                Que.splice(i,1);
            }
        }
    }

    curProcess[0]=firstProcess;
    return firstProcess;
}

//每秒的运行操作
function timeZone(algo){
    console.log("当前运行时间"+timeCount);
    
    //对等待队列进行相应的算法操作
    eval("pop"+algo+"()");
    //popFIFO();

    //对当前元素进行减少服务时间操作
    if(curProcess[0]!=null){
        curProcess[0].needTime--;
        console.log("当前运行元素");
        console.log(curProcess[0]);
    }


    //输出当前等待队列
    console.log("当前等待队列");
    console.log(Que);
    console.log("当前队列长"+Que.length);

    //等待队列为空且当前无运行进程
    if(curProcess[0]==null){
        showWarn("运行结束请刷新页面！",10000);
        //todo 结束操作
        //清除计时方法
        clearInterval(myInterval);
        document.getElementById("clockBtn").innerHTML="运行";
        //清空运行进程
        document.getElementById("cur_process").innerHTML="";
        return;
    }
    showProcess();

    //更新时间
    timeCount++;
    document.getElementById("timecount").innerHTML=timeCount;
}


//运行的时钟对象
var myInterval = setInterval(function(){
    timeZone();
},1000);
//创建时钟时暂停
clearInterval(myInterval);


//开始/暂停时钟
function StopStartClock() {
    var btnValue=document.getElementById("clockBtn").innerHTML;

    var algo=document.getElementById("algo").value;
    
    if(btnValue=="暂停"){
        clearInterval(myInterval);//清除计时方法
        document.getElementById("clockBtn").innerHTML="运行";
    }
    if(btnValue=="运行"){
        myInterval=setInterval("timeZone('"+algo+"')",1000);//设置计时方法
        document.getElementById("clockBtn").innerHTML="暂停";
    }
}

//提示框相关操作
function showWarn(msg,time){
    document.getElementById("warn").innerText=msg;
    document.getElementById("warn").style.display="block";
    setInterval(function(){
        closeWarn();
    },time);
}

function closeWarn(){
    document.getElementById("warn").style.display="none";
}


function createProcess(){

    //获取表单中进程相关属性
    // var new_id=document.getElementById("new_id").value;
    var new_name=document.getElementById("new_name").value;
    var new_arriveTime=document.getElementById("new_arriveTime").value;
    var new_needTime=document.getElementById("new_needTime").value;
    var new_prio=document.getElementById("new_prio").value;
    // var new_num=document.getElementById("new_num").value;

    if(new_name=="" || new_arriveTime=="" || new_needTime=="" || new_prio==""){
        //显示提示信息(填写完整)
        showWarn("请将进程信息填写完整！！！",3000)
        return;
    }


    //创建一个新的进程
    var new_process=new Process(new_name,new_arriveTime,new_needTime,new_needTime,"0","0",new_prio,"0");
    Que.push(new_process);


    //刷新进程列表
    showQue(1);
}


//显示已经创建进程队列到页面中
function showQue(type){
    var workDivs="";
    for(var i=0;i<Que.length;i++){
        var workDiv="<div class='work'>"+Que[i].name+"</div>";
        workDivs+=workDiv;
    }

    if(type==1){
        document.getElementById("que_process").innerHTML=workDivs;
    }else if(type==2){
        document.getElementById("rea_process").innerHTML=workDivs;
    }
}


//显示运行+就绪队列到页面中
function showProcess(){
    document.getElementById("cur_process").innerHTML="<div class='work'>"+curProcess[0].name+"</div>";
    showQue(2);
}



//重置页面(刷新页面)
function reset(){
    // Que=[];
    // curProcess=[];
    // showQue(1);
    // document.getElementById("rea_process").innerHTML="";
    // document.getElementById("que_process").innerHTML="";
    // document.getElementById("cur_process").innerHTML="";
    location.reload();
}

//测试
// $(document).ready(
//     function() {
//         Que.push(new Process("s1","1","2","2","2","0","0","1"));
//         Que.push(new Process("s2","2","2","2","2","0","0","1"));
//         Que.push(new Process("s3","3","2","2","2","0","0","1"));
//         Que.push(new Process("s4","4","2","2","2","0","0","1"));
//         Que.push(new Process("s5","5","2","2","2","0","0","1"));
//         Que.push(new Process("s6","6","2","2","2","0","0","1"));
//     }
// )